package com.common.ui;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;

import com.common.presenter.BasePresenter;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/2/11.
 *  Android 面向对象六大原则：
 *  1、单一职责原则(Single Responsibility Principle)
 *  2、开闭原则(Open Close Principle)
 *  3、里氏替换原则(Liskov Substitution Principle)
 *  4、依赖倒置原则(Dependence Inversion Principle)
 *  5、接口隔离原则(InterfaceSegregation Principle)
 *  6、迪米特原则(Law to Demeter)
 * Created by Administrator on 2016/10/17.
 */
public abstract class BaseActivity<V,P extends BasePresenter> extends AppCompatActivity implements BaseView {
    protected static UIHandler mUIHandler = new UIHandler(Looper.getMainLooper());
    protected P mPresenter;
    private Unbinder mUnbinder;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //绑定布局
        setBaseContentView(savedInstanceState);
        mPresenter = createPresenter();
        if(mPresenter !=null){
            mPresenter.attachView(this);
        }
        init(savedInstanceState);
        setHandler();
        initView();
        initData();
        initEvents();
        ActivityTaskManager.getInstance().addActivity(this);
    }


    private void init(Bundle savedInstanceState){
        mUnbinder = ButterKnife.bind(this);
    }

    protected abstract void initView();
    protected abstract void initData();
    protected abstract void initEvents();
    protected abstract void setBaseContentView(Bundle savedInstanceState);
    protected abstract void handler(Message msg);
    protected abstract P createPresenter();     //创建presenter
    public void jumpToActivity(Class target){
        Intent intent = new Intent(this,target);
        startActivity(intent);
    }

    /**
     * 设置状态栏
     * @param color
     */
    protected void setStatusBarColor(int color){
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            Window window = getWindow();
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
//            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(color);
        }
    }

    private void setHandler() {
        mUIHandler.setIHandler(new UIHandler.IHandler() {
            @Override
            public void handlerMessage(Message msg) {
                handler(msg);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter!=null)
            mPresenter.detachView();
        if (mUnbinder!=null)
            mUnbinder.unbind();
    }
    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
